package com.handa.webShell.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.handa.common.entity.Response;
import com.handa.webShell.entity.DO.CommonCommandsDO;
import com.handa.webShell.entity.DO.CommonCommandsCategoryDO;
import com.handa.webShell.service.ICommonCommandsCategoryService;
import com.handa.webShell.service.ICommonCommandsService;
import com.handa.webShell.utils.TreeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-04-24
 */
@RestController
@RequestMapping("/WebShell/common-commands")
public class CommonCommandsController {

    @Resource
    private ICommonCommandsService commonCommandsService;

    @Resource
    private ICommonCommandsCategoryService commonCommandsCategoryService;

    @GetMapping("/queryAll")
    public Response<List<CommonCommandsDO>> queryAll() {
        List<CommonCommandsDO> res = commonCommandsService.list();
        res.forEach(cmd -> {
            Long catId = cmd.getCategory();
            CommonCommandsCategoryDO byId = commonCommandsCategoryService.getById(catId);
            cmd.setCategoryName(byId.getName());
        });

        return Response.success(res);
    }

    @PostMapping("/query")
    public Response<List<CommonCommandsDO>> query(@RequestBody CommonCommandsDO command) {

        QueryWrapper<CommonCommandsDO> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(command.getCommand())) {
            queryWrapper.like("command", command.getCommand());  // 直接使用列名字符串
        }
        if (StringUtils.isNotBlank(command.getDescription())) {
            queryWrapper.like("description", command.getDescription());
        }
        if (!Objects.isNull(command.getCategory())) {
            List<CommonCommandsCategoryDO> all = commonCommandsCategoryService.list();
            List<Long> allChildIds = TreeUtils.getAllChildIds(all, command.getCategory());

            if (CollectionUtils.isNotEmpty(allChildIds)) {
                queryWrapper.in("category", allChildIds);
            }

        }

        List<CommonCommandsDO> res = commonCommandsService.list(queryWrapper);
        res.forEach(cmd -> {
            Long catId = cmd.getCategory();
            CommonCommandsCategoryDO byId = commonCommandsCategoryService.getById(catId);
            cmd.setCategoryName(byId.getName());
        });

        return Response.success(res);
    }

    @PostMapping("/saveOrUpdate")
    public Response<Void> addItem(@RequestBody CommonCommandsDO command) {

        boolean newFlag = Objects.isNull(command.getId()) || command.getId() <= 0;

        if (newFlag) {
            command.setCreateTime(new Date());
        }
        command.setUpdateTime(new Date());

        boolean b = commonCommandsService.saveOrUpdate(command);

        if (b) {
            return Response.success(newFlag ? "创建命令成功" : "修改命令成功", null);
        } else {
            return Response.error(newFlag ? "创建命令失败" : "修改命令失败");
        }

    }

    @PostMapping("/deleteItem")
    public Response<Void> deleteItem(@RequestParam Integer id) {

        boolean b = commonCommandsService.removeById(id);

        if (b) {
            return Response.success("删除命令成功", null);
        } else {
            return Response.error("删除命令失败");
        }

    }

}

